package com.cloudkeeper.leasing.identity.controller.impl;

import com.cloudkeeper.leasing.base.model.Result;
import com.cloudkeeper.leasing.identity.controller.VillageCadresTermTemporaryController;
import com.cloudkeeper.leasing.identity.domain.VillageCadres;
import com.cloudkeeper.leasing.identity.domain.VillageCadresTermTemporary;
import com.cloudkeeper.leasing.identity.dto.villagecadrestermtemporary.VillageCadresTermTemporaryDTO;
import com.cloudkeeper.leasing.identity.dto.villagecadrestermtemporary.VillageCadresTermTemporarySearchable;
import com.cloudkeeper.leasing.identity.service.VillageCadresService;
import com.cloudkeeper.leasing.identity.service.VillageCadresTermTemporaryService;
import com.cloudkeeper.leasing.identity.vo.VillageCadresTermTemporaryVO;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * 村干部临时表 controller
 * @author asher
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VillageCadresTermTemporaryControllerImpl implements VillageCadresTermTemporaryController {

    /** 村干部临时表 service */
    private final VillageCadresTermTemporaryService villageCadresTermTemporaryService;
    private final VillageCadresService villageCadresService;

    @Override
    public Result<VillageCadresTermTemporaryVO> findOne(@ApiParam(value = "村干部临时表id", required = true) @PathVariable String id) {
        Optional<VillageCadresTermTemporary> villageCadresTermTemporaryOptional = villageCadresTermTemporaryService.findOptionalById(id);
        return villageCadresTermTemporaryOptional.map(villageCadresTermTemporary -> Result.of(villageCadresTermTemporary.convert(VillageCadresTermTemporaryVO.class))).orElseGet(Result::ofNotFound);
    }

    @Transactional
    @Override
    public Result<VillageCadresTermTemporaryVO> add(@ApiParam(value = "村干部临时表 DTO", required = true) @RequestBody @Validated VillageCadresTermTemporaryDTO villageCadresTermTemporaryDTO) {
        villageCadresTermTemporaryService.deleteAllByCadresId(villageCadresTermTemporaryDTO.getCadresId());
        VillageCadresTermTemporary villageCadresTermTemporary = villageCadresTermTemporaryService.save(villageCadresTermTemporaryDTO.convert(VillageCadresTermTemporary.class));
        return Result.ofAddSuccess(villageCadresTermTemporary.convert(VillageCadresTermTemporaryVO.class));
    }

    @Override
    public Result<VillageCadresTermTemporaryVO> update(@ApiParam(value = "村干部临时表id", required = true) @PathVariable String id,
        @ApiParam(value = "村干部临时表 DTO", required = true) @RequestBody @Validated VillageCadresTermTemporaryDTO villageCadresTermTemporaryDTO) {
        Optional<VillageCadresTermTemporary> villageCadresTermTemporaryOptional = villageCadresTermTemporaryService.findOptionalById(id);
        if (!villageCadresTermTemporaryOptional.isPresent()) {
            return Result.ofLost();
        }
        VillageCadresTermTemporary villageCadresTermTemporary = villageCadresTermTemporaryOptional.get();
        BeanUtils.copyProperties(villageCadresTermTemporaryDTO, villageCadresTermTemporary);
        villageCadresTermTemporary = villageCadresTermTemporaryService.save(villageCadresTermTemporary);
        if ("2".equals(villageCadresTermTemporaryDTO.getStatue())){
            //此村干部退休
            // 人员退休标记
            Optional<VillageCadres> optional = villageCadresService.findOptionalById(villageCadresTermTemporaryDTO.getCadresId());
            if (!optional.isPresent()) {
                return Result.of(404, "该村干部不存在");
            }
            // 没有任何职位的时候标记退休
            VillageCadres villageCadres = optional.get();
            villageCadres.setHasRetire("1");
            villageCadresService.save(villageCadres);
        }
        return Result.ofUpdateSuccess(villageCadresTermTemporary.convert(VillageCadresTermTemporaryVO.class));
    }

    @Override
    public Result delete(@ApiParam(value = "村干部临时表id", required = true) @PathVariable String id) {
        villageCadresTermTemporaryService.deleteById(id);
        return Result.ofDeleteSuccess();
    }

    @Override
    public Result<List<VillageCadresTermTemporaryVO>> list(@ApiParam(value = "村干部临时表查询条件", required = true) @RequestBody VillageCadresTermTemporarySearchable villageCadresTermTemporarySearchable,
        @ApiParam(value = "排序条件", required = true) Sort sort) {

        List<VillageCadresTermTemporary> villageCadresTermTemporaryList = villageCadresTermTemporaryService.findAll(villageCadresTermTemporarySearchable);
        List<VillageCadresTermTemporaryVO> villageCadresTermTemporaryVOList = VillageCadresTermTemporary.convert(villageCadresTermTemporaryList, VillageCadresTermTemporaryVO.class);
        return Result.of(villageCadresTermTemporaryVOList);
    }

    @Override
    public Result<Page<VillageCadresTermTemporaryVO>> page(@ApiParam(value = "村干部临时表查询条件", required = true) @RequestBody VillageCadresTermTemporarySearchable villageCadresTermTemporarySearchable,
        @ApiParam(value = "分页参数", required = true) Pageable pageable) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(VillageCadresTermTemporary.class);
        if(villageCadresTermTemporarySearchable.getDistrictId() != null && villageCadresTermTemporarySearchable.getDistrictId().length() == 4){
            //镇
            detachedCriteria.add(Restrictions.eq("parentDistrictId",villageCadresTermTemporarySearchable.getDistrictId()));
            detachedCriteria.add(Restrictions.eq("statue","0"));
        }else if (villageCadresTermTemporarySearchable.getDistrictId() != null && villageCadresTermTemporarySearchable.getDistrictId().length() == 6){
            detachedCriteria.add(Restrictions.eq("districtId",villageCadresTermTemporarySearchable.getDistrictId()));
        }else {
            detachedCriteria.add(Restrictions.eq("statue","1"));
        }
        Integer total = villageCadresTermTemporaryService.getTotalCount(detachedCriteria);
        detachedCriteria.addOrder(Order.desc("createdAt"));
        Page<VillageCadresTermTemporary> villageCadresTermTemporaryPage = villageCadresTermTemporaryService.findAll(detachedCriteria, pageable,total);
        Page<VillageCadresTermTemporaryVO> villageCadresTermTemporaryVOPage = VillageCadresTermTemporary.convert(villageCadresTermTemporaryPage, VillageCadresTermTemporaryVO.class);
        return Result.of(villageCadresTermTemporaryVOPage);
    }

}
